package generator;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tyf on 2018/1/11
 */
public class FreemarkerMapper {
    static Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);

    public static StringWriter formartStr(String resource, Map root) throws IOException, TemplateException {
        cfg.setTemplateLoader(new MyStringTemplateLoader(resource));
        Template template = cfg.getTemplate("", "utf-8");
        StringWriter writer = new StringWriter();
        template.process(root, writer);
        return writer;
    }

    public static void main(String[] args) throws IOException, TemplateException {
        for (int i = 0; i < 10; i++) {
            HashMap root1 = new HashMap();
            HashMap ages = new HashMap();
            ages.put("projectName", "12");
            String s1 = "<project xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" +
                    "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
                    "    <modelVersion>4.0.0</modelVersion>\n" +
                    "\n" +
                    "    <parent>\n" +
                    "        <groupId>com.tyf</groupId>\n" +
                    "        <artifactId>tyf-common</artifactId>\n" +
                    "        <version>3.0.1.RC1</version>\n" +
                    "        <relativePath/>\n" +
                    "    </parent>\n" +
                    "\n" +
                    "    <artifactId>tyf-${projectName}</artifactId>\n" +
                    "    <version><!--current-->3.0.1.RC1</version>\n" +
                    "    <packaging>pom</packaging>\n" +
                    "    <name>tyf::${projectName}</name>\n" +
                    "\n" +
                    "    <modules>\n" +
                    "        <module>tyf-${projectName}-allapp</module>\n" +
                    "        <module>tyf-${projectName}-api</module>\n" +
                    "        <module>tyf-${projectName}-core</module>\n" +
                    "        <module>tyf-${projectName}-web</module>\n" +
                    "    </modules>\n" +
                    "</project>\n";
            System.out.println(s1);
            s1 = formartStr(s1, ages).toString();
            System.out.println(s1);
        }
    }

}
